package kaindorfcompany.webshop.productmanagement.frontend.servlets.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ServletUtil {

    public static void forwardToView(HttpServletRequest request, HttpServletResponse response, String viewName)
            throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/views/" + viewName + ".jsp").forward(request, response);
    }

    public static void redirect(HttpServletResponse response, String location) throws ServletException, IOException {
        response.sendRedirect(location);
    }

    public static void showGeneralError(String message, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        showGeneralError(message, "javascript:goBack()", request, response);
    }

    public static void showGeneralError(String message, String redirect, HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        showGeneralError(message, redirect, "Webshop - Error occured", request, response);
    }

    public static void showGeneralError(String message, String redirect, String title, HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("message", message);
        request.setAttribute("link", redirect);
        request.setAttribute("title", title);
        forwardToView(request, response, "error/generalError");
    }
}
